import { GeneraDataType } from "@/interface";
import dayjs, { Dayjs } from "dayjs";
type RangeValue = [Dayjs, Dayjs];
export default function InitHook(state: GeneraDataType) {
  const { passData, searchForm } = state;
  const ranges = {
    今天: [dayjs().startOf("day"), dayjs().endOf("day")] as RangeValue,
    本月: [dayjs().startOf("month"), dayjs().endOf("month")] as RangeValue
  };
  const init = () => {
    switch (passData.value.type) {
      case "select":
      case "cascader":
        if (!searchForm.value[passData.value.keyName]) searchForm.value[passData.value.keyName] = undefined;
        initSelect();
        break;
      case "dict-select":
        if (!searchForm.value[passData.value.keyName]) searchForm.value[passData.value.keyName] = undefined;
        break;
    }
  };
  // 初始化 下拉
  const initSelect = () => {
    const config = passData.value;
    if (config.optionsRequest && isFunction(config.optionsRequest)) {
      const params = config.optReqParams || {};
      config.optionsRequest(params).then((res: any) => {
        if (config.optReqCb && isFunction(config.optReqCb)) return config.optReqCb(res, config);
        else {
          config.options = res.data;
        }
      });
    }
  };
  // 是否是函数
  const isFunction = (data: any) => {
    return Object.prototype.toString.call(data) === "[object Function]";
  };
  return {
    ranges,
    init
  };
}
